package leetcode_201_300;

import com.sun.org.apache.bcel.internal.generic.ISUB;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_263 {
    public static void main(String[] args) {
//        System.out.println(isUgly(8));
//        System.out.println(isUgly(14));
        System.out.println(isUgly(1000));
        System.out.println(isUgly(0));
    }
    private static boolean isUgly2(int n) {
        if (n < 1)
            return false;
        while (n % 5 == 0){
            n /= 5;
        }
        while (n % 3 == 0){
            n /= 3;
        }
        while (n % 2 == 0){
            n >>= 1;
        }
        return n == 1;
    }
    private static boolean isUgly(int n) {
        if (n == 1)
            return true;
        if (n <= 0)
            return false;
        double m = Math.pow(n, 0.5);
        Set<Integer> set = new HashSet<>();
        set.add(2);
        set.add(3);
        set.add(5);
        for (int i = 2; i <= m; i++) {
            if (n % i == 0 && ((iszhi(i) && !set.contains(i)) || (iszhi(n / i) && !set.contains(n / i)))){
                return false;
            }
        }
        return !iszhi(n) || set.contains(n);
    }
    private static boolean iszhi(int n){
        double m = Math.pow(n, 0.5);
        for (int i = 2; i <= m; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
}
